// StreamDriverLoadApp.cpp : Defines the entry point for the application.  UserMode Version
//

#include "stdafx.h"

#include <devload.h>


int LoadRegistry();
int LoadDriver();
TCHAR szDevKey[] = _T("Drivers\\BuiltIn\\UART1");
TCHAR szCOMPort[] = L"COM1:";

int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
	LPCTSTR   wstream;
	BOOL usedCommandLine = true;

	wprintf(L"StreamDriverLoad App.\r\n");

	wstream = (LPCTSTR)lpCmdLine;

	DWORD cmdlineLength = wcslen(wstream);

	//Default to this stream
	if (cmdlineLength == 0)
	{
		usedCommandLine = false;
	}
	//Check for index no.
	else if (cmdlineLength == 1)
	{
		BYTE index = (BYTE)lpCmdLine[0] - (BYTE) '0';
		if (index <7)
		{
			//Replace 1 in UART1 with index
			szDevKey[wcslen(szDevKey) - 1] = (WCHAR)lpCmdLine[0];
			szCOMPort[wcslen(szCOMPort) - 2] = (WCHAR)lpCmdLine[0];
			usedCommandLine = true;
		}
		//Otherwise default to this stream with index 1
		else
		{
			usedCommandLine = false;
		}
	}
	//Otherwise default to this stream with index 1
	else if (cmdlineLength != 5)
	{
		usedCommandLine = false;
	}

	wprintf(L"Working with registry key: %s\r\n", szDevKey);
	int res = LoadRegistry();
	if (res !=0) return res;
	return LoadDriver();
}
int LoadRegistry() 
{
	
	//TCHAR szDevKey[]  = _T("Drivers\\UART"); 
	//TCHAR szDllName[] = _T("am33x_uart.dll");
	//TCHAR szPrefix[]  = _T("COM");
	HKEY   hKey;
	DWORD  dwDisposition;
	DWORD dwData=DEVFLAGS_LOAD_AS_USERPROC;  //Load the driver in user mode. was 4: Don't load

	DWORD result = 0;

	

	// Open the registry key and add these values
	LONG res1= RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
					szDevKey, 
					0, 
					_T("StreamDriver"), 
					REG_OPTION_VOLATILE, 
					0, 
					NULL, 
					&hKey, 
					&dwDisposition );
	
	if (res1==ERROR_SUCCESS) {
		if (dwDisposition == REG_OPENED_EXISTING_KEY)
		wprintf(L"Registry key openned OK.\r\n");
		else
		{
			wprintf(L"Registry key created new, didn't exist, exiting.\r\n");
			return -1;
		}

	}
	else {
		wprintf(L"Registry key open failed.\r\n");
		return -1;
	}


	// Modify the key specifying the UserMode Load
	/*LONG res4 = RegSetValueEx( hKey,	//subkey handle 
				   _T("Flags"),			// value name 
           0,							// must be zero 
           REG_DWORD,					// value type 
           (LPBYTE) &dwData,			// pointer to value data 
           sizeof(DWORD));				// length of value data */

	LONG res4 = RegDeleteValue(hKey,	//subkey handle 
		_T("Flags"));			// value name 
 

	if (res4==ERROR_SUCCESS) {
		wprintf(L"Registry Flags cleared. OK.\r\n",szDevKey);

	}
	else {
		wprintf(L"Registry Flags clearing failed.\r\n",szDevKey);

		return -4;
	}

	RegFlushKey(hKey);

	LONG res5 = RegCloseKey( hKey );
	if (res5==ERROR_SUCCESS) {
		wprintf(L"Registry key closed OK.\r\n");
	}
	else {
		wprintf(L"Registry key closure failed.\r\n");
		return -5;
	}

	return 0;
}

int LoadDriver()
{
	//TCHAR szDevKey[]  = _T("Drivers\\StreamDriver"); 
	//Assumes registry keys exist

	/*TCHAR szDefaultString[] = _T("");
	REGINI RegIniData[] = { _T("DefaultString"), 
		(LPBYTE)szDefaultString, 
		sizeof(szDefaultString), 
		REG_SZ };*/

	HANDLE hStr = ActivateDeviceEx( szDevKey, NULL,0, NULL );
	wprintf(L"StreamDriverLoad App: Device HANDLE  is %X.\r\n",hStr);
	
	if (hStr == ((HANDLE)0))  //-1 Orev
    {
        wprintf(L"StreamDriverLoad App %s : Load failed.\r\n",szCOMPort);

		 return (int) hStr;
	}else {
		wprintf(L"StreamDriverLoad App %s : Loaded OK.\r\n",szCOMPort);

	}

	return 0;
}

